package com.myfoon.model.ejb;

import com.myfoon.model.entity.Country;

import com.myfoon.model.entity.CountryName;

import com.myfoon.model.helper.CacheHelper;

import com.myfoon.model.helper.ConstantsHelper;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

import java.util.Collections;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "CountryEJB", mappedName = "CountryEJB")
@Local
public class CountryEJBBean implements CountryEJBLocal {
    @PersistenceContext(unitName = "MyfoonPU")
    EntityManager em;

    public CountryEJBBean() {
    }

    public Country persist(Country country) {
        return null;
    }

    public Country merge(Country country) {
        return null;
    }

    public void remove(Country country) {
    }

    public List<CountryName> findAll(String language) {
        NamedCache cache = CacheFactory.getCache("CountryName");
        Object obj = cache.get(language);
        
        List<CountryName> countryNames = null;
        if (obj != null) {
            countryNames = (List<CountryName>)obj;            
        }
        else {
            countryNames =  em.createNamedQuery("Country.findAll").setParameter("language", language).getResultList();            
            cache.put(language, countryNames);            
        }
        
        return countryNames;
    }
}
